package com.example.sx_06.suxuantv.cookie;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import java.util.List;

import okhttp3.Cookie;
import okhttp3.HttpUrl;

public class CookieUtil {
    /**
     * 获取指定URL对应的cookie
     *
     * @param baseUrl
     * @param url
     * @return
     */
    public static List<Cookie> cookies(String baseUrl, String url) {
        //一般手动取出cookie的目的只是交给 webview 等等，非必要情况不要自己操作
        CookieStore cookieStore = OkManager.getInstance().getCookieJar().getCookieStore();
        HttpUrl httpUrl = HttpUrl.parse(baseUrl + url);
        List<Cookie> cookies = cookieStore.getCookie(httpUrl);
        Log.e("cookies: ", httpUrl.host() + "对应的cookie如下：" + cookies.toString());
        return cookies;
    }


    /**
     * 获取指定URL对应的cookie
     *
     * @param baseUrl
     * @param url
     * @return
     */
    public static String cookiesString(String baseUrl, String url) {
        //一般手动取出cookie的目的只是交给 webview 等等，非必要情况不要自己操作
        CookieStore cookieStore = OkManager.getInstance().getCookieJar().getCookieStore();
        HttpUrl httpUrl = HttpUrl.parse(baseUrl + url);
        List<Cookie> cookies = cookieStore.getCookie(httpUrl);
        Log.e("cookies: ", httpUrl.host() + "对应的cookie如下：" + cookies.toString());
     StringBuffer sb =new StringBuffer();
        for (Cookie cookie : cookies) {
            sb.append( cookie.toString());
        }
        String s = sb.toString();

     ///  s= s.replace(";",",");
        Log.e("cookies: ", httpUrl.host() + "对应的cookieStr如下：" +s);

        return s;
    }


    /**
     * 获取所有的cookie
     *
     * @return
     */
    public static List<Cookie> cookieList() {
        CookieStore cookieStore = OkManager.getInstance().getCookieJar().getCookieStore();
        List<Cookie> allCookie = cookieStore.getAllCookie();
        Log.e("所有cookie如下: ", allCookie.toString());
        return allCookie;
    }

    public static String getCookie() {
        CookieStore cookieStore = OkManager.getInstance().getCookieJar().getCookieStore();
        List<Cookie> allCookie = cookieStore.getAllCookie();
        for (int i = 0; i < allCookie.size(); i++) {
            return allCookie.get(i).toString();
        }
        return "";
    }

    /**
     * 删除cookie（这里是全部删除，也可指定的地址删除）
     */
    public static void removeCookie() {
//        HttpUrl httpUrl = HttpUrl.parse(Urls.URL_METHOD);
        CookieStore cookieStore = OkManager.getInstance().getCookieJar().getCookieStore();
//        cookieStore.removeCookie(httpUrl);
        cookieStore.removeAllCookie();
    }

    /**
     * 把cookie交给webview
     * @param context
     * @param url
     * @param cookies
     */
    public static void synchronousWebCookies(Context context, String url, String cookies) {
        if (!TextUtils.isEmpty(url))
            if (!TextUtils.isEmpty(cookies)) {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    CookieSyncManager.createInstance(context);
                }
                CookieManager cookieManager = CookieManager.getInstance();
                cookieManager.setAcceptCookie(true);
                cookieManager.removeSessionCookie();// 移除
                cookieManager.removeAllCookie();
                StringBuilder sbCookie = new StringBuilder();
                sbCookie.append(cookies);
                String cookieValue = sbCookie.toString();
                Log.e("synchronousWebCookies: ", cookieValue);
                cookieManager.setCookie(url, cookieValue);//为url设置cookie

                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    CookieSyncManager.getInstance().sync();
                } else {
                    cookieManager.flush();
                }
            }
    }
}
